package com.project.market.service.impl;

import com.github.pagehelper.PageHelper;
import com.project.market.db.domain.MarketFootprint;
import com.project.market.db.domain.MarketFootprintExample;
import com.project.market.db.domain.MarketUser;
import com.project.market.db.mapper.MarketFootprintMapper;
import com.project.market.service.AdminFootprintService;
import com.project.market.util.MybatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class AdminFootprintServiceImpl implements AdminFootprintService {
    @Override
    public List<MarketFootprint> list(String page, String limit, String sort, String order, String userId, String goodsId) {

        SqlSession session = MybatisUtil.getSession();
        MarketFootprintMapper footprintMapper = session.getMapper(MarketFootprintMapper.class);

        MarketFootprintExample marketFootprintExample = new MarketFootprintExample();
        MarketFootprintExample.Criteria criteria = marketFootprintExample.createCriteria();
        criteria.andDeletedEqualTo(false);

        if (userId != null && !("".equals(userId))) {
            try {
                criteria.andUserIdEqualTo(Integer.valueOf(userId));
            } catch (Exception e) {
                return null;
            }
        }

        if (goodsId != null && !("".equals(goodsId))) {
            try {
            criteria.andGoodsIdEqualTo(Integer.valueOf(goodsId));
            } catch (Exception e) {
                return null;
            }
        }

        marketFootprintExample.setOrderByClause(sort + " " + order);

        PageHelper.startPage(Integer.valueOf(page), Integer.valueOf(limit));

        List<MarketFootprint> marketFootprints = footprintMapper.selectByExample(marketFootprintExample);
        session.commit();
        session.close();
        return marketFootprints;
    }
}
